/*--------------------------------------------------------------------------------------------------
Yatak Operation System Project Copyright 2012-2013
Author : Ilya Churaev
Started : 9 Dec 2012		Updated : 4 Mar 2013
File Name : yatak.h			Purpose : Some mos specific functions
--------------------------------------------------------------------------------------------------*/
#ifndef _YATAK_SYSTEM_H
#define _YATAK_SYSTEM_H

#ifdef __cplusplus
extern "C" {
#endif

#include <yatak/devices.h>
#include <yatak/debug.h>
#include <yatak/regs.h>
#include <yatak/pm.h>
#include <types.h>
#include <cpuid.h>
#include <list.h>
#include <pci.h>

#define KERNEL_CS 0x10
#define KERNEL_DS 0x18
#define USER_CS 0x23
#define USER_DS 0x2b

#define INTR_KERNEL 0x00008E00
#define INTR_USER   0x00008E00 | 0x00006000

#define move_to_user_mode() \
__asm__ __volatile__ ("movl %%esp,%%eax\n\t" \
	"pushl %0\n\t" \
	"pushl %%eax\n\t" \
	"pushfl\n\t" \
	"pushl %1\n\t" \
	"pushl $1f\n\t" \
	"iret\n" \
	"1:\tmovl %0,%%eax\n\t" \
	"mov %%ax,%%ds\n\t" \
	"mov %%ax,%%es\n\t" \
	"mov %%ax,%%fs\n\t" \
	"mov %%ax,%%gs" \
	: /* no outputs */ :"i" (0x2b), "i" (0x23):"ax")

#ifdef __i386__
	#define sti() __asm__ __volatile__ ("sti": : :"memory")
	#define cli() __asm__ __volatile__ ("cli": : :"memory")
	#define nop() __asm__ __volatile__ ("nop")
#else
	#define sti()
	#define cli()
	#define nop()
#endif

#ifdef __i386__
	#define save_flags(x) \
		__asm__ __volatile__("pushfl ; popl %0":"=r" (x): /* no input */ :"memory")
#else
	#define save_flags(x)
#endif 

#ifdef __i386__
	#define restore_flags(x) \
		__asm__ __volatile__("pushl %0 ; popfl": /* no output */ :"r" (x):"memory")
#else
	#define restore_flags(x)
#endif 

#define iret() __asm__ __volatile__ ("iret": : :"memory")

#define _set_gate(gate_addr,type,dpl,addr) \
asm volatile ("movw %%dx,%%ax\n\t" \
	"movw %2,%%dx\n\t" \
	"movl %%eax,%0\n\t" \
	"movl %%edx,%1" \
	:"=m" (*((long *) (gate_addr))), \
	 "=m" (*(1+(long *) (gate_addr))) \
	:"i" ((short) (0x8000+(dpl<<13)+(type<<8))), \
	 "d" ((char *) (addr)),"a" (KERNEL_CS << 16) \
	:"ax","dx")

#define set_intr_gate(n,addr) \
	_set_gate(&idt[n],14,0,addr)

#define set_trap_gate(n,addr) \
	_set_gate(&idt[n],15,0,addr)

#define set_system_gate(n,addr) \
	_set_gate(&idt[n],15,3,addr)

#define set_call_gate(a,addr) \
	_set_gate(&idt[a],12,3,addr)

#define _set_seg_desc(gate_addr,type,dpl,base,limit) {\
	*((gate_addr)+1) = ((base) & 0xff000000) | \
		(((base) & 0x00ff0000)>>16) | \
		((limit) & 0xf0000) | \
		((dpl)<<13) | \
		(0x00408000) | \
		((type)<<8); \
	*(gate_addr) = (((base) & 0x0000ffff)<<16) | \
		((limit) & 0x0ffff); }

#define _set_tssldt_desc(n,addr,limit,type) \
__asm__ __volatile__ ("movw $" #limit ",%1\n\t" \
	"movw %%ax,%2\n\t" \
	"rorl $16,%%eax\n\t" \
	"movb %%al,%3\n\t" \
	"movb $" type ",%4\n\t" \
	"movb $0x00,%5\n\t" \
	"movb %%ah,%6\n\t" \
	"rorl $16,%%eax" \
	: /* no output */ \
	:"a" (addr+0xc0000000), "m" (*(n)), "m" (*(n+2)), "m" (*(n+4)), \
	 "m" (*(n+5)), "m" (*(n+6)), "m" (*(n+7)) \
	)

#define set_tss_desc(n,addr) _set_tssldt_desc(((char *) (n)),((int)(addr)),103,"0x89")
#define set_ldt_desc(n,addr,size) \
	_set_tssldt_desc(((char *) (n)),((int)(addr)),((size << 3) - 1),"0x82")
	
#define CHECK_FLAG(flags,bit)   ((flags) & (1 << (bit)))

// Check if flag
// if flag = 0 interrups disable
#define check_if_flag (flag)	\
	asm volatile ("				\
			pushf;				\
			pop %%eax;			\
			and $0x200, %%eax;	\
			mov %%eax, %0" : "=r"(flag));
 

/*	:"a" (addr+0xc0000000), "m" (*(n)), "m" (*(n+2)), "m" (*(n+4)), \
	 "m" (*(n+5)), "m" (*(n+6)), "m" (*(n+7)) \*/

struct system_info							// stores information about a system
{
	struct cpu_info cpuinfo;				// CPU
	list_devices_t * devices;				// List for devices
	PCI_BIOS_t * pci_bios;					// BIOS
	uint32 time_work;						// Time of system work
	size_t memory;							// RAM in KB
};

typedef struct system_info system_info_t;

void PrintSysInfo (system_info_t * mos_info);

void panic(registers_t* r);
void panic_assert(const char *file, uint line32, const char *desc);
void beep();

#ifdef __cplusplus
}
#endif

#endif
